﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public delegate void AlarmSelectionSaved(bool isSaved , string ProductVersion , string KeyAlarmSelection , string LampAlarmSelection);
    public partial class Form_AlarmSelection : Form
    {
        public int formLanguageIndex { get; set; } = -1;

        public event AlarmSelectionSaved OnAlarmSelectionSaved;

        private string _ProductVersion;
        private string _selectionstring;
        private string _productname;
        private string _lampAlarmString;
        private List<string> equipid=new List<string>();
        public Form_AlarmSelection(string ProductVersion , string productName,string selectionstring,string lampalarmstring)
        {
            InitializeComponent();
            _ProductVersion = ProductVersion;
            _selectionstring = selectionstring;
            _lampAlarmString = lampalarmstring;
            lbl_SetTitle.Text = productName + "  关联警报设置";
            _productname = productName;
        }

        private void Form_AlarmSelection_Load(object sender , EventArgs e)
        {
            LoadInterface();
        }
        private void LoadInterface()
        {
            foreach (string item in NetLine.Equip.Keys)
            {
                cmb_EquipSelect.Items.Add(NetLine.Equip[item].EquipName);
                equipid.Add(item);
            }
            cmb_EquipSelect.SelectedIndex = 0;


            grdAlarmSelection.Columns["alarmid"].Width = grdAlarmSelection.Columns["alarmlevel"].Width = grdAlarmSelection.Columns["alarmselect"].Width = grdAlarmSelection.Columns["lampalarm"].Width = 100;
            grdAlarmSelection.Columns["alarmname"].Width = grdAlarmSelection.Width - 430;
            List<string> selectedId=new List<string>( _selectionstring.Split(new char[] { '|'},StringSplitOptions.RemoveEmptyEntries));
            List<string> lampalarmId=new List<string>( _lampAlarmString.Split(new char[] { '|'},StringSplitOptions.RemoveEmptyEntries));

            DataGridViewRow row;

            for (int i = 0 ; i < NetLine.alarm.Count ; i++)
            {
                row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = NetLine.alarm[i].AlarmID });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = NetLine.alarm[i].AlarmName });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = NetLine.alarm[i].AlarmLevel });
                if(selectedId.IndexOf(NetLine.alarm[i].AlarmID.ToString())==-1)
                    row.Cells.Add(new DataGridViewCheckBoxCell() { Value =false});
                else
                    row.Cells.Add(new DataGridViewCheckBoxCell() { Value =true });
                if (lampalarmId.IndexOf(NetLine.alarm[i].AlarmID.ToString()) == -1)
                    row.Cells.Add(new DataGridViewCheckBoxCell() { Value = false });
                else
                    row.Cells.Add(new DataGridViewCheckBoxCell() { Value = true });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = NetLine.alarm[i].AlarmEquipID });
                if (NetLine.alarm[i].AlarmEquipID != equipid[0]) row.Visible = false;
                grdAlarmSelection.Rows.Add(row);
            }
        }


        private void ShowEquipAlarm(string eid)
        {
            for (int i = 0 ; i < grdAlarmSelection.Rows.Count ; i++)
            {
                if (grdAlarmSelection.Rows[i].Cells["alarmequipid"].Value!=null && grdAlarmSelection.Rows[i].Cells["alarmequipid"].Value.ToString() == eid)
                    grdAlarmSelection.Rows[i].Visible = true;
                else
                    grdAlarmSelection.Rows[i].Visible = false;
            }
        }
        private void btn_AlarmSelectCancel_Click(object sender , EventArgs e)
        {
            this.Dispose();
        }

        private void btn_AlarmSelectConfirm_Click(object sender , EventArgs e)
        {
            string keyalarmSelection;
            string lampalarmSelection;
            bool isok =ModifyProductALarmSelection(_ProductVersion,out keyalarmSelection,out lampalarmSelection);
            if (isok)
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[4], _productname, formLanguageIndex);
                //fdMessage.ShowMsg(_productname + "的关联警报已经更新！", "关联警报" , 1 , 0);
                //netForms.fmDesign.LoadProductInfos();
            else
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[5], _productname, formLanguageIndex);
                //fdMessage.ShowMsg(_productname + "警报关联失败，请检查数据库！", "关联警报" , 1 , 0);
            if (OnAlarmSelectionSaved != null)
                OnAlarmSelectionSaved.Invoke(isok , _ProductVersion , keyalarmSelection, lampalarmSelection);
            this.Dispose();
        }

        private bool ModifyProductALarmSelection(string productSerial , out string KeyAlarmSelection , out string LampAlarmSelection)
        {
            StringBuilder sbkeyalarmselection=new StringBuilder();
            StringBuilder sblampalarmselection=new StringBuilder();
            for (int i = 0 ; i < grdAlarmSelection.Rows.Count ; i++)
            {
                if (Convert.ToBoolean(grdAlarmSelection.Rows[i].Cells["alarmselect"].Value) == true)
                    sbkeyalarmselection.Append(grdAlarmSelection.Rows[i].Cells["alarmid"].Value.ToString()).Append("|");
                if (Convert.ToBoolean(grdAlarmSelection.Rows[i].Cells["lampalarm"].Value) == true)
                    sblampalarmselection.Append(grdAlarmSelection.Rows[i].Cells["alarmid"].Value.ToString()).Append("|");
            }
            KeyAlarmSelection = sbkeyalarmselection.ToString();
            LampAlarmSelection = sblampalarmselection.ToString();
            return DBOperate.ChangeProductAlarmSelection(productSerial , sbkeyalarmselection.ToString() , sblampalarmselection.ToString()); ;
        }

        private void cmb_EquipSelect_SelectedIndexChanged(object sender , EventArgs e)
        {
            ShowEquipAlarm(equipid[cmb_EquipSelect.SelectedIndex]);
        }
    }
}
